package z.explorer.core.storage.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapSession;

import z.explorer.core.storage.Param;
import z.explorer.core.storage.dao.ParamDAO;
import java.util.Map;

import javax.sql.DataSource;

public class ParamDAOImpl extends SqlMapClientDaoSupport implements ParamDAO {

	@Override
	public List<Param> searchParamByExe(int table_id, int exe_seq) {
		// TODO Auto-generated method stub
		HashMap<String, Integer> paramMap = new HashMap<String, Integer>();
		paramMap.put("table_id", table_id);
		paramMap.put("exe_seq", exe_seq);

		return (List<Param>) getSqlMapClientTemplate().queryForList(
				"paramByExe", paramMap);
	}

	@Override
	public void insertParam(List<Param> list, int tableId,/* int exeSeq, */
	int op_id, String uname) {
		// TODO Auto-generated method stub
		SqlMapClient smc = getSqlMapClient();
		Connection conn = null;
		SqlMapSession session = null;
		try {
			DataSource ds = smc.getDataSource();
			conn = ds.getConnection();
			conn.setAutoCommit(false);
			session = smc.openSession(conn);
			// smc.startTransaction();
			int exeSeq = (Integer) smc.queryForObject("getExeCount", tableId);
			exeSeq = exeSeq + 1;
			HashMap<String, Object> responder = new HashMap<String, Object>();
			responder.put("responder", uname);
			responder.put("table_id", tableId);
			session.update("updateResponder", responder);

			HashMap<String, Object> exeParam = new HashMap<String, Object>();
			exeParam.put("table_id", tableId);
			exeParam.put("exe_seq", exeSeq);
			exeParam.put("op_id", op_id);
			session.insert("insertExe", exeParam);

			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			for (int i = 0; i < list.size(); i++) {
				paramMap.clear();
				paramMap.put("table_id", tableId);
				paramMap.put("exe_seq", exeSeq);
				paramMap.put("param_name", list.get(i).getParam_name());
				paramMap.put("param_value", list.get(i).getParam_value());
				paramMap.put("param_type", list.get(i).getParam_type());
				session.insert("insertParam", paramMap);
			}

			// smc.commitTransaction();
			conn.commit();
		} catch (Exception e) {
			if (null != conn)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			e.printStackTrace();
		} finally {
			try {
				if (null != session)
					session.close();
				if (null != conn)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	@Override
	public void updateParam(List<Param> list, int tableId, int exeSeq,
			String uname) {
		// TODO Auto-generated method stub
		SqlMapClient smc = getSqlMapClient();
		Connection conn = null;
		SqlMapSession session = null;
		try {
			DataSource ds = smc.getDataSource();
			conn = ds.getConnection();
			conn.setAutoCommit(false);
			session = smc.openSession(conn);
			// smc.startTransaction();
			HashMap<String, Object> responder = new HashMap<String, Object>();
			responder.put("responder", uname);
			responder.put("table_id", tableId);
			session.update("updateResponder", responder);

			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			for (int i = 0; i < list.size(); i++) {
				paramMap.clear();
				paramMap.put("table_id", tableId);
				paramMap.put("exe_seq", exeSeq);
				paramMap.put("param_name", list.get(i).getParam_name());
				paramMap.put("param_value", list.get(i).getParam_value());
				paramMap.put("param_type", list.get(i).getParam_type());
				session.update("updateParam", paramMap);
			}
			// smc.commitTransaction();
			conn.commit();
		} catch (Exception e) {
			if (null != conn)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			e.printStackTrace();
		} finally {
			try {
				if (null != session)
					session.close();
				if (null != conn)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	@Override
	public int getExeCount(int tableId) {
		// TODO Auto-generated method stub
		return (Integer) getSqlMapClientTemplate().queryForObject(
				"getExeCount", tableId);

	}

	@Override
	public void removeExeSeq(int tableId, int exeSeq, String uname) {
		// TODO Auto-generated method stub
		SqlMapClient smc = getSqlMapClient();
		Connection conn = null;
		SqlMapSession session = null;

		try {
			DataSource ds = smc.getDataSource();
			conn = ds.getConnection();
			conn.setAutoCommit(false);
			session = smc.openSession(conn);
			// smc.startTransaction();
			HashMap<String, Object> responder = new HashMap<String, Object>();
			responder.put("responder", uname);
			responder.put("table_id", tableId);
			session.update("updateResponder", responder);

			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("exe_seq", exeSeq);
			paramMap.put("table_id", tableId);
			session.delete("deleteParamByExe", paramMap);
			session.delete("deleteExe", paramMap);
			session.update("decreaseExeSeq", paramMap);
			session.update("decreaseParamExeSeq", paramMap);
			// smc.commitTransaction();
			conn.commit();

		} catch (Exception e) {
			if (null != conn)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			e.printStackTrace();
		} finally {
			try {
				if (null != session)
					session.close();
				if (null != conn)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void moveExeSeq(int tableId, int exeSeq, int direction, String uname) {
		// TODO Auto-generated method stub
		SqlMapClient smc = getSqlMapClient();
		Connection conn = null;
		SqlMapSession session = null;
		try {
			DataSource ds = smc.getDataSource();
			conn = ds.getConnection();
			conn.setAutoCommit(false);
			session = smc.openSession(conn);
			// smc.startTransaction();
			int count = (Integer) smc.queryForObject("getExeCount", tableId);
			if (direction == -1 && exeSeq == 1) {
				// session.close();
				// conn.close();
				return;
			} else if (direction == 1 && exeSeq == count) {
				// session.close();
				// conn.close();
				return;
			}

			HashMap<String, Object> responder = new HashMap<String, Object>();
			responder.put("responder", uname);
			responder.put("table_id", tableId);
			session.update("updateResponder", responder);

			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("old_exe_seq", exeSeq);
			paramMap.put("new_exe_seq", -1);
			paramMap.put("table_id", tableId);
			session.update("updateParamExeSeq", paramMap);
			session.update("updateExeSeq", paramMap);

			paramMap.clear();
			paramMap.put("old_exe_seq", exeSeq + direction);
			paramMap.put("new_exe_seq", exeSeq);
			paramMap.put("table_id", tableId);
			session.update("updateParamExeSeq", paramMap);
			session.update("updateExeSeq", paramMap);

			paramMap.clear();
			paramMap.put("old_exe_seq", -1);
			paramMap.put("new_exe_seq", exeSeq + direction);
			paramMap.put("table_id", tableId);
			session.update("updateParamExeSeq", paramMap);
			session.update("updateExeSeq", paramMap);

			// smc.commitTransaction();
			conn.commit();
		} catch (Exception e) {
			if (null != conn)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			e.printStackTrace();
		} finally {
			try {
				if (null != session)
					session.close();
				if (null != conn)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public String getCode(int table_id, int count, String p_run_date) {
		SqlMapClientTemplate template = getSqlMapClientTemplate();
		StringBuilder code= new StringBuilder("");
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		for (int i = 1; i <= count; i++) {
			paramMap.clear();
			paramMap.put("table_id", table_id);
			paramMap.put("exe_seq", i);
			paramMap.put("p_run_date", p_run_date);
			String v=(String) getSqlMapClientTemplate().queryForObject("selectCode",
					paramMap);
			code.append(v);
			code.append("\n");
		}
		return code.toString();
	}
}
